package com.hxc.controller;

import com.hxc.anno.Log;
import com.hxc.entity.Clazz;
import com.hxc.entity.ClazzQueryParam;
import com.hxc.entity.PageBean;
import com.hxc.entity.Result;
import com.hxc.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    /**
     * 查询班级信息，分页查询
     * */
    @GetMapping
    public Result getClazzs(ClazzQueryParam param) {
        log.info("接受查询请求，查询参数：{}",param);
        PageBean pageBean = clazzService.getClazz(param);
        return Result.success(pageBean);
    }
    /**
     * 新增班级信息
     * */
    @Log
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        log.info("新增班级：{}",clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }
    /**
     * 根据id查询班级
     * */
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable int id) {
        Clazz clazz = clazzService.getClazzById(id);
        return Result.success(clazz);
    }
    /**
     * 修改班级信息
     * */
    @Log
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();
    }
    /**
     * 删除班级
     * */
    @Log
    @DeleteMapping("{id}")
    public Result deleteClazz(@PathVariable int id) {
        log.info("请求删除id={}的班级",id);
        Boolean b = clazzService.deleteClazz(id);
        if (b) {
            return Result.success();
        }else {
            return Result.error("此班级下有学员，不能删除");
        }
    }
    /**
     *
     * 查询所有班级
     */
    @GetMapping("/list")
    public Result getClazzList() {
        List<Clazz> clazzes = clazzService.getAllClazz();
        return Result.success(clazzes);
    }

}
